package main

import (
	"fmt"
	"gitee.com/duan_shengjie/category/domain/repository"
	"gitee.com/duan_shengjie/category/handler"
	category "gitee.com/duan_shengjie/category/proto/category"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/util/log"
	"github.com/micro/go-plugins/registry/consul/v2"
)

func main() {
	//consulConfig, err := common.GetConsulConfig("127.0.0.1",9500,"micro/config")
	//if err != nil {
	//	fmt.Println("初始化consul失败",err)
	//}
	consulRegisty := consul.NewRegistry(
		func(options *registry.Options) {
			options.Addrs = []string{"127.0.0.1:8500"}
		})

	srv := micro.NewService(
		micro.Name("go.micro.service.category"),
		micro.Version("latest"),
		micro.Address("127.0.0.1:8082"),
		micro.Registry(consulRegisty),
	)

	srv.Init()

	db, err := gorm.Open("mysql", "root:123456@/micro?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		fmt.Println("数据库连接失败--", err.Error())
	}
	defer db.Close()
	db.SingularTable(true)

	rp := repository.NewCategoryRepository(db)
	rp.InitTable()

	err = category.RegisterCategoryHandler(srv.Server(), &handler.Category{CategoryRepository: rp})
	if err != nil {
		log.Error(err)
	}

	srv.Run()

	// 数据库
}
